package com.hdoit.apollo.web.purchase.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.purchase.mapper.PurchaseCollocationMapper;
import com.hdoit.apollo.logical.purchase.model.Purchase;
import com.hdoit.apollo.logical.purchase.model.PurchaseCollocation;
import com.hdoit.apollo.logical.purchase.model.PurchaseCollocationStat;
import com.hdoit.apollo.logical.purchase.service.IPurchaseCollocationService;
import com.hdoit.apollo.logical.purchase.service.IPurchaseService;
import com.hdoit.apollo.logical.viewmodel.purchase.PurchaseCollocationSearch;
import com.hdoit.apollo.logical.viewmodel.purchase.PurchaseSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.data.message.PagerModel;

@Controller
@RequestMapping(value = "/api/v1/purchase/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/purchase")
public class PurchaseApi extends BaseApi{
	
	@Autowired
	private PurchaseCollocationMapper purchaseCollocationMapper;
	
	@Autowired
	private IPurchaseService purchaseService;
	
	@Autowired
	private IPurchaseCollocationService purchaseCollocationService;
	
	private final String queryPurchaseStatApi="queryPurchaseStat";
	
	private final String queryPurchaseListByScrollApi="queryPurchaseListByScroll";
	
	private final String queryPurchaseDetailApi="queryPurchaseDetail";
	
	@RequestMapping(value = queryPurchaseStatApi)
    @ApiOperation(notes = queryPurchaseStatApi, httpMethod = "GET", value = "获取客户订单列表")
    public @ResponseBody String queryPurchaseStat(PurchaseCollocationSearch search){
    	PurchaseCollocationStat purchaseCollocationStat = purchaseCollocationMapper.queryPurchaseStat(search);
    	ResponseModel rm=new ResponseModel();
    	rm.put("purchaseCollocationStat",purchaseCollocationStat);
    	return rm.toJson();
    }
	
	@RequestMapping(value = queryPurchaseDetailApi)
    @ApiOperation(notes = queryPurchaseDetailApi, httpMethod = "GET", value = "采购单详情")
    public @ResponseBody String queryPurchaseDetail(String purchaseId){
    	List<PurchaseCollocation> purchaseCollocations = purchaseCollocationService.queryListByPurchaseId(purchaseId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("purchaseCollocations",purchaseCollocations);
    	return rm.toJson();
    }
	
	@RequestMapping(value = queryPurchaseListByScrollApi)
    @ApiOperation(notes = queryPurchaseListByScrollApi, httpMethod = "POST", value = "滚动查找订单信息列表")
    public @ResponseBody String queryPurchaseListByScroll(PurchaseSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}

    	List<Purchase> purchases=purchaseService.queryListMP(search, pagerModel);
    	
    	rm.put("purchases",purchases);
    	return rm.toJson();
    }
}
